from typing import List


class Solution:
    def findFarmland(self, land: List[List[int]]) -> List[List[int]]:
        m, n = len(land), len(land[0])

        ans = []

        for i0 in range(m):
            for j0 in range(n):
                if land[i0][j0] == 0:
                    continue
                if i0 > 0 and land[i0 - 1][j0] == 1:  # 当前位置上方是1，不是左上角
                    continue
                if j0 > 0 and land[i0][j0 - 1] == 1:  # 当前位置左侧是1，不是左上角
                    continue

                # 寻找右侧、下方边界
                i1 = i0
                while i1 + 1 < m and land[i1 + 1][j0] == 1:
                    i1 += 1
                j1 = j0
                while j1 + 1 < n and land[i0][j1 + 1] == 1:
                    j1 += 1

                ans.append([i0, j0, i1, j1])

        return ans


if __name__ == "__main__":
    print(Solution().findFarmland(land=[[1, 0, 0], [0, 1, 1], [0, 1, 1]]))  # [[0,0,0,0],[1,1,2,2]]
    print(Solution().findFarmland(land=[[1, 1], [1, 1]]))  # [[0,0,1,1]]
    print(Solution().findFarmland(land=[[0]]))  # []
